﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;

namespace SportsStore.Domain.Concrete
{
    public class EFProductRepository : IProductRepository
    {
        private EFDbContext context = new EFDbContext();

        public IQueryable<Product> Products
        { 
            get 
            { 
                return context.Products; 
            } 
        }

        public void SaveProduct(Product product)
        {
            if (product.ProductID == 0)
            {
                context.Products.Add(product);
            }
            else
            {
                Product prod =  context.Products.FirstOrDefault(p => p.ProductID == product.ProductID);

                if (prod != null)
                {
                    prod.Name = product.Name;
                    prod.Category = product.Category;
                    prod.Price = product.Price;
                    prod.Description = product.Description;
                    prod.ImageData = product.ImageData;
                    prod.ImageMimeType = product.ImageMimeType;
                }
            }

            context.SaveChanges();
        }

        public void DeleteProduct(Product product)
        {
            if (product != null)
            {
                context.Products.Remove(product);
                context.SaveChanges();
            }
        }
    }
}
